package com.example.yichuangyun.mvvm.base;

import android.view.View;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.mvvm.di.retrofit.RetrofitHelper;
import com.jakewharton.rxbinding2.view.RxView;

import java.util.concurrent.TimeUnit;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class BaseViewModel extends ViewModel {

    private CompositeDisposable mCompositeDisposable;
    public RetrofitHelper mRetrofitHelper;
    public MutableLiveData<String> mErrorData = new MutableLiveData<>();

    public void setRetrofitHelper(RetrofitHelper retrofitHelper) {
        mRetrofitHelper = retrofitHelper;
    }

    public void onDelayClick(View view, Consumer<Object> consumer) {
        addSubscribe(RxView.clicks(view).throttleFirst(Constant.DELAY_CLICK, TimeUnit.MILLISECONDS).subscribe(consumer));
    }

    public void onDelayClick(View view, Consumer<Object> consumer, long delay) {
        addSubscribe(RxView.clicks(view).throttleFirst(delay, TimeUnit.MILLISECONDS).subscribe(consumer));
    }

    public void addSubscribe(Disposable paramDisposable) {
        if (this.mCompositeDisposable == null) {
            this.mCompositeDisposable = new CompositeDisposable();
        }
        this.mCompositeDisposable.add(paramDisposable);
    }

    protected void unSubscribe() {
        if (this.mCompositeDisposable != null) {
            this.mCompositeDisposable.dispose();
        }
    }
}
